import { message } from 'antd';
import {
    getChargeList,
    addStoreCharge,
    updateStoreCharge,
    deleteStoreCharge,
} from '../../services/store';

const CHARGE_INFO = {
    model: 2,
    modelName: '畅玩',
};
export default {
    namespace: 'meal',
    state: {
        data: null,
        editorData: {},
        visible: false,
    },
    subscriptions: {
        setup(args) {
            const { dispatch, history } = args;
            history.listen(({ pathname }) => {
                if (pathname === '/marketing/package') {
                    dispatch({
                        type: 'init',
                    });
                }
            });
        }
    },
    effects: {
        * init(payload, { put }) {
            yield put({
                type: 'load',
            });
        },
        * load(payload, { put, call, select }) {
            const { storeId } = yield select((state => state.app.user));
            const { data, err, code } = yield call(getChargeList, {storeId, model: 2});
            if (code === '0000') {
                yield put({
                    type: 'setState',
                    payload: {
                        data,
                    },
                });
            } else {
                console.log(err);
            }
        },
        * editorOk({ payload: { err, isNew, values } }, { call, put, select }) {
            if (err) {
                return;
            }
            let res;
            const { storeId } = yield select(state => state.app.user);
            const body = {
                ...values,
                ...CHARGE_INFO,
                storeId,
            };
            if (!isNew) {
                res = yield call(updateStoreCharge, body);
            } else {
                res = yield call(addStoreCharge, body);
            }
            const { code, msg, err: e } = res;
            if (code === '0000') {
                message.success(msg);
                yield [
                    put({
                        type: 'editorVisible',
                    }),
                    put({
                        type: 'init',
                    }),
                ];
            } else {
                message.error(msg || e);
            }
        },
        * removeData({ rechargeId }, { call, put }) {
            const { code, msg, err } = yield call(deleteStoreCharge, rechargeId);
            if (code === '0000') {
                yield [
                    put({
                        type: 'init',
                    }),
                    call(message.success, msg)
                ];
            } else {
                message.error(msg, err);
            }
        },
    },
    reducers: {
        setState(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        editorVisible(state, { payload: { editorData } = {} }) {
            // 隐藏时不清数据,避免因为隐藏动作慢而界面显示为新建
            if (editorData) {
                return {
                    ...state,
                    visible: !!editorData,
                    editorData,
                };
            } else {
                return {
                    ...state,
                    visible: false,
                };
            }
        },
    }
};
